package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.documentNumberPojo;
import com.example.service.documentNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class documentNumberController {

    @Autowired
    private documentNumberService documentNumberService;

    // Post query for all document numbers
    @PostMapping("/queryAllDocumentNumber")
    public Result queryAllDocumentNumber() {
        try {
            List<documentNumberPojo> documentNumberPojoList = documentNumberService.queryAll();
            System.out.println("Query successful, found " + documentNumberPojoList.size() + " records");
            return Result.success(documentNumberPojoList);
        } catch (Exception e) {
            System.err.println("Query failed: " + e.getMessage());
            return Result.error(500, "查询失败: " + e.getMessage()); // 使用500作为错误码
        }
    }

    // Post method to add a new document number
    @PostMapping("/addDocumentNumber")
    public Result addDocumentNumber(@RequestBody documentNumberPojo documentNumberPojo) {
        System.out.println("Adding Document Number: " + documentNumberPojo);
        documentNumberService.insertOne(documentNumberPojo);
        System.out.println("Addition successful");
        return Result.success();
    }

    // Post method to edit an existing document number
    @PostMapping("/editDocumentNumber")
    public Result editDocumentNumber(@RequestBody documentNumberPojo documentNumberPojo) {
        documentNumberService.editDocumentNumber(documentNumberPojo);
        System.out.println("Edit successful");
        return Result.success("Edit successful");
    }
}
